<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\model\Area;
use app\common\model\CurriculumCate;
use app\common\model\CurriculumOrder;
use app\common\model\Evaluate;
use app\common\model\HistorySearch;
use app\common\model\Order as OrderModel;
use app\common\model\ProductCate;
use app\common\model\User;
use app\common\tool\Result;
use GuzzleHttp\HandlerStack;
use think\facade\Cache;
use think\facade\View;
use think\response\Json;
use app\common\model\Curriculum as CurriculumModel;
use app\common\model\Chapter as ChapterModel;
use app\common\model\Section as SectionModel;

class Curriculum extends Base
{


    /**
     * get: 课程列表
     * path: curriculumList
     * method: curriculumList
     * param: keyword - {string} 搜索关键词
     * param: page - {string} 页数
     * param: cate - {string} 分类
     */
    public function curriculumList(): \think\Response
    {
        $param = $this->request->param();
        $that = CurriculumModel::field('id,title,price,introduction,images,frequency');
        $map = [];
        if (isset($param['keyword'])) {
            $that->whereLike('title', '%' . $param['keyword'] . '%');
            $token = $this->request->header('token');
            if ($token) {
                HistorySearch::setHistory($param['keyword'], $this->user->id);
            }
        }
        $that->where($map);
        $that->where(['is_del'=>0,'status'=>1]);

        $that->order('sort', 'asc')->order('create_time', 'desc');
        $that->page(intval($param['page'] ?? 1), 20);
        $list = $that->select();
        if (isset($list)) {
            return Result::success('获取成功', $list);
        } else {
            return Result::fail('获取失败');
        }
    }



    /**
     * get: 课程详情
     * path: details
     * method: details
     * param: id - {string} id
     */
    public function details()
    {
        $id = $this->request->param('id');
        $info = CurriculumModel::where('id', $id)->field('id,price,teacher,content,images')->find();
        $info->content = handleRichText($info->content);
        $info->images = explode(',', $info->images);
        CurriculumModel::whereIn('id', $id)->inc('frequency',1)->update();//增加观看次数
        if (isset($info)) {
            return Result::success('获取成功', $info);
        } else {
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 课程目录
     * path: catalogue
     * method: catalogue
     * param: id - {string} id
     */
    public function catalogue()

    {
        $id = $this->request->param('id');
        $list = ChapterModel::where('c_id', $id)->field('id,title')->order('sort', 'asc')->select();
        if (isset($list)) {
            return Result::success('获取成功', $list);
        } else {
            return Result::fail('获取失败');
        }
    }


    /**
     * get: 课节
     * path: section
     * method: section
     * param: c_id - {string} c_id
     */
    public function section()

    {
        $c_id = $this->request->param('c_id');
        $list = SectionModel::where('ch_id', $c_id)->field('id,title,video')->order('sort', 'asc')->select();
        if (isset($list)) {
            return Result::success('获取成功', $list);
        } else {
            return Result::fail('获取失败');
        }
    }


    /**
     * post: 购买课程
     * path: submit
     * method: submit
     * param: id - {string} id
     */
    public function submit()
    {
        $param = $this->request->post();
//        if ($this->user->is_lock) {
//            return Result::fail('账号已锁定');
//        }
//        $num = \app\common\model\Address::where(['uid'=>$this->user->id,'default'=>1])->count();
//        if(!$num)
//        {
//            return Result::fail('请先设置默认地址');
//        }
        $submit = CurriculumModel::submit($param, $this->user);
        if ($submit['code'] > 0) {
            return Result::success($submit['msg']);
        } else {
            return Result::fail($submit['msg']);
        }
    }





}

